package com.skyz.wrap.manager;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.skyz.base.util.NetWorkUtil;
import com.skyz.base.util.SharedPreferenceUtils;

import java.util.List;
import java.util.Map;

public class TokenManager {
    private static final String KEY_TOKEN = "KEY_TOKEN";
    private static final String KEY_CUNR_USERNAME = "KEY_CUNR_USERNAME";

    private TokenManager() {
    }


    private static class InstanceHolder {
        private static final TokenManager instance = new TokenManager();
    }

    public static TokenManager getInstance() {
        return InstanceHolder.instance;
    }

    public String getToken(Context context){
        return   SharedPreferenceUtils.getStringValue(context, KEY_TOKEN, "");
    }
    public void setToken(Context context,String token){
        SharedPreferenceUtils.setValue(context, KEY_TOKEN, token);
    }

    public String getUserName(Context context){
        return   SharedPreferenceUtils.getStringValue(context, KEY_CUNR_USERNAME, "");
    }
    public void setUserName(Context context,String username){
        SharedPreferenceUtils.setValue(context, KEY_CUNR_USERNAME, username);
    }

    public boolean isLogin(Context context) {
        String token = SharedPreferenceUtils.getStringValue(context, KEY_TOKEN, null);
        return token != null;
    }

    public boolean isHasTokenAndNetUsable(Context context){
      return   NetWorkUtil.isNetSystemUsable(context)&&TokenManager.getInstance().isLogin(context);
    }

    public void setSysTa1TypeDetail(Context context,Map<String,String> map) {
        String json = new Gson().toJson(map);
        SharedPreferenceUtils.setValue(context, "SysTa1TypeDetail",json );
    }
    public Map<String,String>  getSysTa1TypeDetail(Context context){
        String json  =SharedPreferenceUtils.getStringValue(context, "SysTa1TypeDetail", "");
        Map<String,String> map = null;
        try {
            map = new Gson().fromJson(json,new TypeToken<Map<String,String>>() {}.getType());
        }catch (Exception e){
            e.printStackTrace();
        }
        return  map;
    }
}
